package com.keson.fujian.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keson.fujian.util.FileUtil;

@Controller
public class IndexController {
	
	@RequestMapping("/")
	public String index() {
		return "index";
	}
	
	@RequestMapping("/td")
	@ResponseBody
	public JSONArray td() {
		String p040 = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\06叠加数据_JM_P040.txt"));
		String[] p = p040.split("\n");
		
		String dislist = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\06叠加数据_JM_DisList.txt"));
		String[] dis = dislist.split("\n");
		
		JSONArray array = new JSONArray();
		for (int i = 0; i < dis.length; i++) {
			ArrayList<Float> values = new ArrayList<>();
			float disBase = Float.parseFloat(dis[i].trim());
			String[] value = Arrays.copyOfRange(p[i].trim().split(","),  0,  12000);
			
			for (int j = 0; j < value.length; j++) {
				if(j%10==0) {
					values.add(Float.parseFloat(value[j].trim())*2+disBase);
				}
			}
			array.add(values);
		}
		
		return array;
	}
	
	@RequestMapping("/station")
	@ResponseBody
	public JSONObject station() {
		String[] error = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\09异常台站.txt")).split("\n");
		JSONArray errorArray = new JSONArray();
		for (String string : error) {
			String[] info = string.split("\t");
			JSONObject obj = new JSONObject();
			obj.put("name", info[0]);
			obj.put("lat", Float.parseFloat(info[1]));
			obj.put("lon", Float.parseFloat(info[2]));
			errorArray.add(obj);
		}
		
		float maxDis = 0;
		String[] online = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\03已接收台站列表.txt")).split("\n");
		JSONArray onlineArray = new JSONArray();
		for (String string : online) {
			String[] info = string.split("\t");
			JSONObject obj = new JSONObject();
			obj.put("name", info[0]);
			obj.put("lat", Float.parseFloat(info[1]));
			obj.put("lon", Float.parseFloat(info[2]));
			onlineArray.add(obj);
			
			float dis = Float.parseFloat(info[3]);
			if(dis>maxDis) {
				maxDis = dis;
			}
		}
		
		String[] offline = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\01站点清单.txt")).split("\n");
		JSONArray offlineArray = new JSONArray();
		for (String string : offline) {
			String[] info = string.split("\t");
			JSONObject obj = new JSONObject();
			obj.put("name", info[0]);
			obj.put("lat", Float.parseFloat(info[1]));
			obj.put("lon", Float.parseFloat(info[2]));
			if(!onlineArray.contains(obj)) {
				offlineArray.add(obj);
			}
		}
		
		String[] source = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\02气枪震源位置.txt")).split("\t");
		JSONObject sourceobj = new JSONObject();
		sourceobj.put("name", source[0]);
		sourceobj.put("lat", Float.parseFloat(source[1]));
		sourceobj.put("lon", Float.parseFloat(source[2]));
		
		String[] route = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\04航线轨迹.txt")).split("\n");
		JSONArray routeArray = new JSONArray();
		for (String string : route) {
			String[] info = string.split("\t");
			
			JSONObject obj = null;
			boolean has = false;
			for (int i = 0; i < routeArray.size(); i++) {
				if(routeArray.getJSONObject(i).getString("name").equals(info[0])) {
					has = true;
					obj = routeArray.getJSONObject(i);
					break;
				}
			}
			if(!has) {
				obj = new JSONObject();
				obj.put("name", info[0]);
				obj.put("lat1", Float.parseFloat(info[1]));
				obj.put("lon1", Float.parseFloat(info[2]));
			}else {
				obj.put("lat2", Float.parseFloat(info[1]));
				obj.put("lon2", Float.parseFloat(info[2]));
			}
			routeArray.add(obj);
		}
		
		String[] gun = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\08枪控文件.txt")).split("\n");
		String mag = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\10等效单次震级.txt"));
		
		String[] offset = FileUtil.readFileContent(new File("H:\\地震文档\\福建局界面优化\\07震源位置偏差统计.txt")).split("\n");
		JSONArray offsetArray = new JSONArray();
		for (String string : offset) {
			String[] info = string.split("\t");
			JSONObject obj = new JSONObject();
			obj.put("lat", Float.parseFloat(info[0]));
			obj.put("lon", Float.parseFloat(info[1]));
			offsetArray.add(obj);
		}
		
		JSONObject json = new JSONObject();
		json.put("maxDis", maxDis);
		json.put("error", errorArray);
		json.put("online", onlineArray);
		json.put("offline", offlineArray);
		json.put("gun", gun);
		json.put("mag", mag);
		json.put("source", sourceobj);
		json.put("route", routeArray);
		json.put("offset", offsetArray);
		
		return json;
	}
}
